package com.xinz.config;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.ibatis.session.SqlSession;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.xinz.config.dao.ConfigRoomMapper;
import com.xinz.config.model.ConfigRoom;

public class ConfigHelper {

	private static final Logger log = LoggerFactory.getLogger(ConfigHelper.class);
	
	// 房间信息<Room, ConfigRoom>
	private static Map<Integer, ConfigRoom> configRoomMap = new HashMap<Integer, ConfigRoom>();
	
	public static void init() {
		try {
			// 房间初始化
			initConfigRoom();
		}
		catch(Exception e) {
			log.error("init config error:" + e);
		}
	}
	
	/**
	 * 房间
	 */
	private static void initConfigRoom() {
		SqlSession session = MapperManager.getResSqlMapper().openSession();
		try {
			ConfigRoomMapper mapper = session.getMapper(ConfigRoomMapper.class);
			List<ConfigRoom> configRooms = mapper.selectByExample(null);
			for(ConfigRoom configRoom : configRooms) {
				configRoomMap.put(configRoom.getRoom(), configRoom);
			}
		}
		catch(Exception e) {
			e.printStackTrace();
		}
		finally {
			session.close();
		}
	}
	
	/**
	 * 获取房间配置全量数据
	 * @return
	 */
	public static Map<Integer, ConfigRoom> getAllConfigRoomMap() {
		return configRoomMap;
	}
	
	/**
	 * 根据房间号获取房间配置
	 * @param room
	 * @return
	 */
	public static ConfigRoom getConfigRoom(int room) {
		return configRoomMap.get(room);
	}
}
